import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
  selector: 'app-demo8',
  templateUrl: './demo8.component.html',
  styles: [``],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Demo8Component {
  sliderValue = [];
  range = [];

  format = 'MM-dd hh:mm';
  step = [1, "Hour"];
  space = 60;

  times = [];
  selectValue: { [key: string]: any };
  index = 0;

  ngOnInit() {
    this.initData();
    this.selectTime(this.times[1]);
  }

  onChange(event) {
    this.sliderValue = event;
    console.log('变化后的值是', event)
  };

  sliderDragEnd(event) {
    console.log('滑块拖动结束后的值', event);
  }

  initData() {
    this.times = [
      {
        label: '最近1年',
        value: 'LAST_YEAR',
        isLeaf: true
      },
      {
        label: '最近1月',
        value: 'LAST_Month 1',
        isLeaf: true
      }
    ];
    this.selectValue = this.times[1];
  }
  
  selectTime(event) {
    const curDate = new Date();
    let preDate = new Date();
    let timeArr;
    switch (event.value) {
      case 'LAST_YEAR':
        this.format = 'yyyy-MM-dd HH:mm';
        this.step = [5, 'Day'];
        this.space = 90;
        preDate.setFullYear(curDate.getFullYear() - 1);
        timeArr = [preDate, curDate];
        this.range = this.sliderValue = timeArr;
        break;
      case 'LAST_Month 1':
        this.format = 'MM-dd HH:mm';
        this.step = [1, 'Day'];
        this.space = 60;
        preDate.setMonth(curDate.getMonth() - 1);
        timeArr = [preDate, curDate];
        this.range = this.sliderValue = timeArr;
        break;
    }
  }
}
